Chat-Application
A chat application allows users to interact or communicate by texting, audio, and video. It can be useful for social or commercial activities.
In this assignment, students are required to write a Chat Program based on Windows Socket Programming C++. The program should allow users to chat concurrently on the Internet (i.e., User A can send encrypted messages to User B, and vice versa). You can download the sample programs (namely, “server.cpp” and “client.cpp”) from the Web site. The program should consist of the following modules:
- User management: provide functions to add, delete, or group users;
- Group chat: allow users to chat as a group;
- Management of connections: support at least three concurrent connections.
User Management: Implemented By Server
- Server: Store the User Information
Group chat: Group started by Client
- Server Provide all users for Client User to select.
Management of connections: Give every User a Threads
[!abstract]- Content Chinese version 起初,我们的客户端实现仅采用了单线程方式,消息的接收和发送是交替进行的。这种实现方式存在一个显著的问题:客户端无法同时进行消息的接收和发送操作,而是只能在完成接收后进行发送,反之亦然。这导致了客户端在等待接收或发送消息时会发生阻塞,影响用户体验。
为了克服这一问题,我们引入了多线程技术。在客户端中,我们分别为消息的接收和发送操作创建了独立的线程,从而实现了这两项操作的并发处理。这一改进使得客户端可以同时进行消息的接收和发送,提高了响应速度和用户体验。
在服务器端,我们引入了互斥锁(mutex)来确保数据传输的正确性和线程安全性。当多个用户向同一个用户发送消息时,服务器会将这些消息推送到该用户的消息队列中,并在这一过程中使用互斥锁来保证消息的有序和完整传输。这样,服务器能够在多用户环境下稳定、高效地管理消息队列,确保每个用户都能正确收到发送给他们的消息。
I'm going to talk some details about how we implemented this chat room application.
Overview Of the Data structure
- Sock: Client socket
- messageQueue: Queue to store messages to be sent
- queueMutex: Mutex to protect the message queue
- queueCond: Condition variable to notify the send thread
Multithreading on both client and server side
- Initially, our client was single-threaded, handling message receiving and sending alternately.
- This caused a problem: the client or server couldn't receive and send messages concurrently, leading to blocking and a poor user experience.
To solve this, we introduced multithreading. Separate threads for receiving and sending to improve responsiveness and user experience.
Message queuing on the server side
- On the server side, we introduced a mutex to ensure data transmission accuracy and thread safety.
- When multiple users send messages to the same user, the server pushes these messages into the receiver's message queue using a mutex to lock the process.
- This ensures orderly and complete message transmission.
These changes enabled the server to efficiently manage message queues in a multi-user environment, ensuring each user correctly receives their messages.